package vip.xiaonuo.megvii.service;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import org.apache.http.client.HttpClient;
import org.springframework.stereotype.Service;
import vip.xiaonuo.core.pojo.response.ErrorResponseData;
import vip.xiaonuo.core.pojo.response.ResponseData;
import vip.xiaonuo.digest.DigestHttpClient;
import vip.xiaonuo.digest.DigestHttpUtil;
import vip.xiaonuo.megvii.BaseConfig;
import vip.xiaonuo.megvii.ResponseDefinition;
import vip.xiaonuo.megvii.config.model.ApiResponse;
import vip.xiaonuo.megvii.facegroups.FaceGroupCodeDefinition;
import vip.xiaonuo.megvii.facegroups.delete.FaceDeleteCenter;
import vip.xiaonuo.megvii.facegroups.model.FaceGroup;
import vip.xiaonuo.megvii.facegroups.model.FaceGroupDelete;
import vip.xiaonuo.modular.devicemegvii.entity.DeviceMegvii;
import vip.xiaonuo.modular.devicemegvii.service.DeviceMegviiService;
import vip.xiaonuo.modular.megviifacegroup.entity.MegviiFaceGroup;
import vip.xiaonuo.modular.megviifacegroup.param.MegviiFaceGroupParam;
import vip.xiaonuo.modular.megviifacegroup.service.MegviiFaceGroupService;

import javax.annotation.Resource;

@Service
public class DeviceMegviiFaceGroupService extends DeviceMegviiBaseService{

    @Resource
    private DeviceMegviiService deviceMegviiService;

    @Resource
    private MegviiFaceGroupService faceGroupService;

    @Resource
    private FaceDeleteCenter strategy;


    /**
     * 创建人脸分组
     * @param megviiFaceGroupParam
     * @return
     */
    public ApiResponse addFaceGroup(MegviiFaceGroupParam megviiFaceGroupParam) {
        if(megviiFaceGroupParam==null || megviiFaceGroupParam.getDwDeviceMegviiId()==null){
            return ApiResponse.failed(-1, ResponseDefinition.DATA_NOT_EXIST);
        }
        long deviceId = megviiFaceGroupParam.getDwDeviceMegviiId();
        DeviceMegvii device = deviceMegviiService.getById(deviceId);
        String url = super.getMachAddress(device);
        if(url==null || url.length()==0){
            return ApiResponse.failed(-1,ResponseDefinition.CONFIG_WRONG);
        }

        HttpClient client = DigestHttpClient.getDigestHttpClient(device);
        FaceGroup group = new FaceGroup(megviiFaceGroupParam.getGroupName());
        ApiResponse res = DigestHttpUtil.sendPost(client,url + BaseConfig.URL_FACE_GROUP , JSON.toJSONString(group));
        int code = res.getCode();
        if(code==0){
            return ApiResponse.success();
        }else{
            return ApiResponse.failed(code,res.getMessage());
        }
    }


    public ResponseData deleteFaceGroup(FaceGroupDelete param) {
        if(param==null || param.getDwDeviceMegviiId()==null){
            return ErrorResponseData.error(-1, ResponseDefinition.DATA_NOT_EXIST);
        }
        long deviceId = param.getDwDeviceMegviiId();
        DeviceMegvii device = deviceMegviiService.getById(deviceId);
        String url = super.getMachAddress(device);
        if(url==null || url.length()==0){
            return ErrorResponseData.error(-1,ResponseDefinition.CONFIG_WRONG);
        }
        HttpClient client = DigestHttpClient.getDigestHttpClient(device);

        String requestUrl = url + BaseConfig.URL_FACE_GROUP_DELETE.replace("{groupName}",param.getGroupName());
        DeleteMode mode = new DeleteMode(param);
        ApiResponse res = DigestHttpUtil.sendDelete(client,requestUrl , JSON.toJSONString(mode));
        int code = res.getCode();
        if(code == 0 || code == FaceGroupCodeDefinition._code_104301){ // 返回成功 返回分组不存在时，当成成功处理
            if(code == FaceGroupCodeDefinition._code_104301){ // 因为不知道服务端是怎么处理的，当返回分组不存在时，删除分组，删除分组关联
                mode.setDeleteAllFace(1);
            }
            this.doDeleteFaceGroup(mode,param);
        }else {
            return new ErrorResponseData(code, FaceGroupCodeDefinition.getMessage(code));
        }
        return ResponseData.success();
    }


    private void doDeleteFaceGroup(DeleteMode mode,FaceGroupDelete param) {
        MegviiFaceGroup faceGroup = faceGroupService.getById(param.getId());
        int deleteMode = mode.getDeleteAllFace();
        strategy.doDelete(deleteMode,faceGroup);
    }

    @Data
    class DeleteMode {
        private int deleteAllFace;

        public DeleteMode(FaceGroupDelete param) {
            this.deleteAllFace = param.getDeleteMode();
        }

    }
}
